package main

import (
	"net/http"

	"code4u-ide/packages/controller"
	"code4u-ide/packages/handler"
	"encoding/json"

	"github.com/gin-gonic/gin"
)

//全局变量
var engine *gin.Engine

func init() {
	//gin.SetMode(gin.ReleaseMode)
	engine = gin.Default()

	//static.NewAssetsFS(engine, assets.AssetDir, assets.Asset, "").LoadStatic()

}

func main() {

	engine.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "static/index.html", nil)
		controller.TestDB()
	})

	engine.GET("/dbeditor.html", func(c *gin.Context) {
		c.HTML(http.StatusOK, "static/html/dbeditor/dbeditor.html", nil)
		controller.TestDB()
	})

	engine.GET("/logiceditor.html", func(c *gin.Context) {
		c.HTML(http.StatusOK, "static/html/logiceditor/logiceditor.html", nil)
	})

	engine.GET("/uieditor.html", func(c *gin.Context) {
		c.HTML(http.StatusOK, "static/html/uieditor/uieditor.html", nil)
	})

	// 编译器命令接口
	engine.GET("/cmd/:cmd", func(c *gin.Context) {
		c.Header("Access-Control-Allow-Origin", "*")
		controller.ExcuteCmd(c.Param("cmd"))
		c.JSON(http.StatusOK, gin.H{
			"msg":  "success",
			"code": http.StatusOK,
		})
	})

	// 编辑器接口
	engine.POST("/editor/:editor/:option", func(c *gin.Context) {
		c.Header("Access-Control-Allow-Origin", "*")
		editor := c.Param("editor")
		option := c.Param("option")
		data := c.DefaultPostForm("data", "")
		var err error
		var result interface{}
		switch editor {
		case "db":
			result, err = controller.DBEditFile(option, data)
		case "lc":
			err = controller.LCEditFile(option, data)
		default:
			err = nil
		}
		rtnMsg := "success"
		rtnCode := http.StatusOK
		if err != nil {
			rtnMsg = err.Error()
			rtnCode = 600
		}
		c.JSON(http.StatusOK, gin.H{
			"msg":    rtnMsg,
			"code":   rtnCode,
			"result": result,
		})
	})

	// 项目管理接口
	engine.POST("/project/:oper", func(c *gin.Context) {
		c.Header("Access-Control-Allow-Origin", "*")
		oper := c.Param("oper")
		data := c.DefaultPostForm("data", "")
		var c4ufile controller.C4uFile
		json.Unmarshal([]byte(data), &c4ufile)
		rtnFile, projectFiles, err := controller.ProjectMgr(oper, &c4ufile)
		rtnMsg := "success"
		rtnCode := http.StatusOK
		if err != nil {
			rtnMsg = err.Error()
			rtnCode = 600
		}
		if projectFiles != nil {
			c.JSON(http.StatusOK, gin.H{
				"msg":  rtnMsg,
				"code": rtnCode,
				"result": gin.H{
					"projectName": "c4uWeb",
					"files":       projectFiles,
				},
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"msg":    rtnMsg,
				"code":   rtnCode,
				"result": rtnFile,
			})
		}
	})

	//初始化一个项目
	engine.GET("/init/:userName/:proName", handler.InitProject)

	// swagger:route POST /create/:type/:userName/:proName 项目管理  CreateArgs
	// 创建文件(夹)
	// responses:
	// 200: ResultResp
	engine.POST("/create/:type/:userName/:proName", handler.Create)
	// swagger:route POST /save/:userName/:proName 项目管理 SaveFileArgs
	// 保存文件内容
	// responses:
	// 200: ResultResp
	engine.POST("/save/:userName/:proName", handler.SaveFile)

	engine.POST("/format/:userName/:proName", handler.ExecCMD)

	engine.Run(":8989")

}
